
(function () {
    var core = ecui,
        util = core.util,
        ui = ecui.ui,
        dom = ecui.dom;

    var auditStates = [
        { 'value': '', 'code': '全部' },
        { 'value': '0', 'code': '草稿' },
        { 'value': '1', 'code': '待审核' },
        { 'value': '2', 'code': '驳回' },
        { 'value': '3', 'code': '通过' },
        { 'value': '4', 'code': '已中止' },
        { 'value': '5', 'code': '撤回' }
    ];
    Object.assign(NS.data, {
        // routes: ['', '#/approval/detail.order', '#/approval/detail.contract', '#/approval/detail.play', '#/approval/detail.sponsor', '#/approval/detail.musician'],
        todoStatus: {
            auditState: {
                value: '',
                title: '状态',
                select: '',
                items: [
                    { 'value': '', 'code': '全部' },
                    { 'value': '0', 'code': '草稿' },
                    { 'value': '1', 'code': '待审核' },
                    { 'value': '2', 'code': '驳回' },
                    { 'value': '3', 'code': '通过' },
                    { 'value': '4', 'code': '已中止' },
                    { 'value': '5', 'code': '撤回' }
                ]
            },
            module: {
                value: '',
                title: '类型',
                select: '',
                items: [
                    { 'value': '0', 'code': '全部' },
                    { 'value': '1', 'code': '包场' },
                    { 'value': '2', 'code': '分票' },
                    { 'value': '3', 'code': '商务' },
                    { 'value': '4', 'code': '自办' },
                    { 'value': '5', 'code': '定场' },
                    { 'value': '6', 'code': '结算单' }
                ]
            }
        }
    });
    Object.assign(NS.data, {
        'todoSortBarConfigs': [
            { type: 'status', typeName: '筛选', active: false, target: 'status_selected', selects: NS.data.todoStatus /*, number: '0'*/ }
        ]
    });
    Object.assign(
        NS.ui,
        {
            ToDoListTab: ecui.inherits(
                mao.ui.Tab,
                {
                    ontitleclick: function (event) {
                        if (this.getSelected() !== event.item) {
                            if (event.index === 1) {
                                var listView = ecui.query(function (item) { return item instanceof mao.ui.AppListView && dom.contain(this.getItem(1).getContainer(), item.getMain()); }.bind(this));
                                ecui.dispatchEvent(listView[0], 'refresh');
                            }
                        } else {
                            if (this.getItems().indexOf(event.item) !== 1) {
                                this.setSelected(0);
                            }
                            return false;
                        }
                        mao.ui.Tab.prototype.ontitleclick.call(this, event);
                    },
                    toggleCloseBtn: function (num, showCloseBtn) {
                        var closeSearchBtn = ecui.get('close-search-' + num),
                            sortBtn = ecui.get('sort-btn-' + num);
                        if (closeSearchBtn) {
                            closeSearchBtn[showCloseBtn ? 'show' : 'hide']();
                        }
                        if (sortBtn) {
                            sortBtn[showCloseBtn ? 'hide' : 'show']();
                        }
                    }
                }
            ),
            CloseSearchBtn: core.inherits(
                ui.Control,
                {
                    onclick: function (event) {
                        util.timer(function () {
                            this.clickhandler(event);
                        }, 10, this);
                    },
                    clickhandler: function (event) {
                        var num = this.$ID.split('-').pop(),
                            tab = this.getParent().getParent(),
                            searchText = ecui.get('search-text-' + num);
                        if (searchText) {
                            // core.setFocused();
                            tab.setSelected(0);
                            tab.toggleCloseBtn(num, false);
                        }
                        if (event) {
                            event.stopPropagation();
                        }
                    },
                    init: function (event) {
                        ui.Control.prototype.init.call(this, event);
                        this.clickhandler();
                    }
                }
            ),
            ToDoSearchText:core.inherits(
                NS.ui.SearchText,
                function (el, options) {
                    this.hContainer = ecui.$('todo_history_container');
                    this.listContainer = dom.next(this.hContainer);
                    mao.ui.SearchText.call(this, el, options);
                    // 添加取消按钮
                    // dom.insertHTML(el, 'BEFOREEND', '<div class="ui-search-close">取消</div>');
                    core.$fastCreate(this.Close, dom.last(el), this, {});
                },
                {
                    $click: function (event) {
                        NS.ui.SearchText.prototype.$click.call(this, event);
                        // event.stopPropagation();
                    },
                    onfocus: function () {
                        var tab = this.getParent().getParent(),
                            num = this.$ID.split('-').pop();
                        tab.toggleCloseBtn(num, true);
                    }
                }
            )
        }
    );
    ecui.esr.addRoute('list', {
        model: [''],
        main: 'approval_todo-list',
        view: 'todo.list',
        queryUrl: '@FORM /erp-management/todo/event/list?todoListSearchForm&todoFilterListForm',
        queryUrl1: '@FORM /erp-management/todo/event/list?todoListSearchForm&todoFilterListForm&todoSearchListForm',
        frame: true,
        weight: 3,
        children: ['todo.historyList'],
        onbeforerequest: function (context) {
            var title = ['', '已发起', '待处理', '已处理', '抄送我'];
            ecui.esr.setData('todoListTitle', title[context.tag]);
            mao.util.setTitle(title[context.tag]);
            
            context.num = Date.now() + '' + Math.ceil(Math.random() * 10000);
        },
        onbeforerender: function (context) {
            if (context.tag === '2') {
                NS.data.todoStatus.auditState.items = auditStates.filter(function (item) { return ['', '0', '1'].indexOf(item.value) > -1 });
            } else if (context.tag === '3') {
                NS.data.todoStatus.auditState.items = auditStates.filter(function (item) { return ['', '2', '3', '4', '5'].indexOf(item.value) > -1 });
            } else {
                NS.data.todoStatus.auditState.items = auditStates;
            }
        },
        onafterrender: function (context) {
            var todoListView = ecui.get('todo-list-view');
            var todoSearchListView = ecui.get('todo-search-list-view');

            todoSearchListView.setUrlName('queryUrl1');

            ecui.dispatchEvent(todoListView, 'refresh');
        }
    });
    // 已审批的搜索历史
    core.esr.addRoute('historyList', {
        model: [
            'historys@GET /erp-management/search/history/list'
        ],
        main: 'todo_history_container',
        view: 'history_list',
        frame: true,
        weight: 3,
        onbeforerender: function (context) {
            // console.log(context);
        },
        onafterrender: function () {
        },
        oncached: function () {
        }
    });
}());

